﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DVDFilmoteka.src;

namespace DVDFilmoteka.Forms
{
    public partial class CreateTemplate : Form
    {
        public CreateTemplate()
        {
            InitializeComponent();
        }

        private void CreateTemplate_Load(object sender, EventArgs e)
        {
            comboBoxMedia.Items.Clear();
            comboBoxMedia.Items.AddRange(Form1Servant.loadDataFromXML(Form1Servant.CONFIG, "//root/mmedium/*").ToArray());
            if (comboBoxMedia.Items.Count > 0) comboBoxMedia.SelectedIndex = 0;

            comboBoxQuality.Items.Clear();
            comboBoxQuality.Items.AddRange(Form1Servant.loadDataFromXML(Form1Servant.CONFIG, "//root/mkvalita/*").ToArray());
            if (comboBoxQuality.Items.Count > 0) comboBoxQuality.SelectedIndex = 0;

            comboBoxCompresion.Items.Clear();
            comboBoxCompresion.Items.AddRange(Form1Servant.loadDataFromXML(Form1Servant.CONFIG, "//root/mkomprese/*").ToArray());
            if (comboBoxCompresion.Items.Count > 0) comboBoxCompresion.SelectedIndex = 0;

            comboBoxContainer.Items.Clear();
            comboBoxContainer.Items.AddRange(Form1Servant.loadDataFromXML(Form1Servant.CONFIG, "//root/mkontejner/*").ToArray());
            if (comboBoxContainer.Items.Count > 0) comboBoxContainer.SelectedIndex = 0;

            comboBoxLanguage.Items.Clear();
            comboBoxLanguage.Items.AddRange(Form1Servant.loadDataFromXML(Form1Servant.CONFIG, "//root/mjazyk/*").ToArray());
            if (comboBoxLanguage.Items.Count > 0) comboBoxLanguage.SelectedIndex = 0;

            comboBoxChanelCount.Items.Clear();
            comboBoxChanelCount.Items.AddRange(Form1Servant.loadDataFromXML(Form1Servant.CONFIG, "//root/mpocetKanalu/*").ToArray());
            if (comboBoxChanelCount.Items.Count > 0) comboBoxChanelCount.SelectedIndex = 0;

            comboBoxSize.Items.Clear();
            comboBoxSize.Items.AddRange(Form1Servant.loadDataFromXML(Form1Servant.CONFIG, "//root/mvelikost/*").ToArray());
            if (comboBoxSize.Items.Count > 0) comboBoxSize.SelectedIndex = 0;

            if (TemplateManager.Instance.AvailableTemplateNames.Count <= 1) buttonTemplateList.Enabled = false;
        }

        /// <summary>
        /// Zmena typu media
        /// </summary>
        private void handleMediaTypeChanged(object sender, EventArgs e)
        {
            if (comboBoxMedia.SelectedIndex < 0 || comboBoxQuality.SelectedIndex < 0) return;

            // Vybrano DVD
            if ("DVD".Equals(comboBoxMedia.Items[comboBoxMedia.SelectedIndex]) && "DVD-Video".Equals(comboBoxQuality.Items[comboBoxQuality.SelectedIndex]))
            {
                comboBoxCompresion.Enabled = false;
                comboBoxCompresion.SelectedIndex = comboBoxCompresion.Items.IndexOf("Mpeg2");
                comboBoxContainer.Enabled = false;
                comboBoxContainer.SelectedIndex = comboBoxContainer.Items.IndexOf("VOB");

                return;
            }

            // Vybrano videoCD
            if ("CD".Equals(comboBoxMedia.Items[comboBoxMedia.SelectedIndex]) && "CD-Video".Equals(comboBoxQuality.Items[comboBoxQuality.SelectedIndex]))
            {
                comboBoxCompresion.Enabled = false;
                comboBoxCompresion.SelectedIndex = comboBoxCompresion.Items.IndexOf("Mpeg");
                comboBoxContainer.Enabled = false;
                comboBoxContainer.SelectedIndex = comboBoxContainer.Items.IndexOf("Jiný");

                return;
            }

            // HD DVD
            if ("HD-DVD".Equals(comboBoxMedia.Items[comboBoxMedia.SelectedIndex]) && "HDDVD-Video".Equals(comboBoxQuality.Items[comboBoxQuality.SelectedIndex]))
            {
                comboBoxCompresion.Enabled = false;
                comboBoxCompresion.SelectedIndex = comboBoxCompresion.Items.IndexOf("Jiná");
                comboBoxContainer.Enabled = false;
                comboBoxContainer.SelectedIndex = comboBoxContainer.Items.IndexOf("Jiný");

                return;
            }

            // BD
            if ("Blue-ray".Equals(comboBoxMedia.Items[comboBoxMedia.SelectedIndex]) && "BD-Video".Equals(comboBoxQuality.Items[comboBoxQuality.SelectedIndex]))
            {
                comboBoxCompresion.Enabled = false;
                comboBoxCompresion.SelectedIndex = comboBoxCompresion.Items.IndexOf("H.264");
                comboBoxContainer.Enabled = false;
                comboBoxContainer.SelectedIndex = comboBoxContainer.Items.IndexOf("Jiný");

                return;
            }

            // Videokazeta
            if ("VHS".Equals(comboBoxMedia.Items[comboBoxMedia.SelectedIndex]))
            {
                comboBoxQuality.Enabled = false;
                comboBoxQuality.SelectedIndex = comboBoxQuality.Items.IndexOf("Jiná");
                comboBoxCompresion.Enabled = false;
                comboBoxCompresion.SelectedIndex = comboBoxCompresion.Items.IndexOf("Jiná");
                comboBoxContainer.Enabled = false;
                comboBoxContainer.SelectedIndex = comboBoxContainer.Items.IndexOf("Jiný");

                return;
            }


            comboBoxQuality.Enabled = true;
            comboBoxCompresion.Enabled = true;
            comboBoxContainer.Enabled = true;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxName.Text))
            {
                MessageBox.Show(this, "Musíte vyplnit název šablony!", Form1Servant.AssemblyName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (TemplateManager.Instance.AvailableTemplateNames.Contains(textBoxName.Text))
            {
                DialogResult result = MessageBox.Show(this, "Šablona s tímto názvem již existuje. Má se upravit?", Form1Servant.AssemblyName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                if (result == DialogResult.No)
                {
                    this.Close();
                }
                else if (result == DialogResult.Cancel)
                {
                    return;
                }
            }

            MediaTemplate template = new MediaTemplate();
            template.compression = comboBoxCompresion.SelectedItem as String;
            template.container = comboBoxContainer.SelectedItem as String;
            template.channelCount = comboBoxChanelCount.SelectedItem as String;
            template.language = comboBoxLanguage.SelectedItem as String;
            template.medium = comboBoxMedia.SelectedItem as String;
            template.quality = comboBoxQuality.SelectedItem as String;
            template.resolution = textBoxResolution.Text;
            template.size = comboBoxSize.SelectedItem as String;

            TemplateManager.Instance.AddOrUpdateTemplate(textBoxName.Text, template);

            this.Close();
        }

        private void buttonTemplateList_Click(object sender, EventArgs e)
        {
            FormTemplateList dialog = new FormTemplateList();
            dialog.ShowDialog(this);

            if (dialog.SelectedTemplate != null) textBoxName.Text = dialog.SelectedTemplate;

            if (TemplateManager.Instance.AvailableTemplateNames.Count <= 1) buttonTemplateList.Enabled = false;
        }
    }
}
